package cn.catsdad.util;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.resource.ClassPathResource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * @author xuyl
 * @version V1.0.0 2024/12/31
 * @since V100R001
 */
public class ResourceUtil {

    public static <T> T readResource(String resKey, Class<T> clazz) {
        ClassPathResource resource = new ClassPathResource("config.yml");
        Properties properties = new Properties();
        try {
            properties.load(resource.getReader(StandardCharsets.UTF_8));
            String property = properties.getProperty(resKey);
            return Convert.convert(clazz, property);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T readOrDefault(String resKey, Class<T> type, T defaultValue) {
        T result = readResource(resKey, type);
        return result == null ? defaultValue : result;
    }

}
